package com.g5kj.coupon.ssm.service.impl;

import com.g5kj.coupon.ssm.common.Result;
import com.g5kj.coupon.ssm.dao.ArticleDao;
import com.g5kj.coupon.ssm.model.ArticleEntity;
import com.g5kj.coupon.ssm.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by apple on 2017/5/17.
 */
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleDao articleDao;

    @Override
    public Result addArticle(ArticleEntity entity) {

        Result check=this.check(entity);
        if(!check.isOk()) return check;

        entity.setCreate_time(new Date());
        int resId=articleDao.addArticel(entity);
        return entity.getId()>0&& resId==1?Result.ok(Integer.valueOf(entity.getId())):Result.fail("添加失败");
    }

    public Result check(ArticleEntity entity){
        if(StringUtils.isEmpty(entity.getTitle())){
            return Result.fail("标题不能为空");
        }
        if(StringUtils.isEmpty(entity.getBody())){
            return Result.fail("内容不能为空");
        }

        return Result.ok(entity);

    }

    public Result update(ArticleEntity entity){
        Result check=this.check(entity);
        if(!check.isOk())return check;
        entity.setUpdate_time(new Date());
        int res=articleDao.update(entity);
        return res>0? Result.ok("修改成功"):Result.fail("修改失败");
    }

    @Override
    public int delete(List<Integer> id) {
        return 0;
    }

    @Override
    public List<ArticleEntity> list(int page, String startTime, String lastTime, String keyValue, String order, int sort) {
        return articleDao.getList(page,startTime,lastTime,keyValue,order,sort);
    }

}
